package com.cskaoyan.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author stone
 * @date 2022/03/24 15:41
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    @RequestMapping("file")
    public String upload(MultipartFile myfile) throws IOException {//input标签的name属性对应的就是请求参数名，请求参数名要和形参名一致
        String name = myfile.getName(); //获得请求参数的名字 → myfile
        String originalFilename = myfile.getOriginalFilename();//获得原始文件名，上传的文件的名字
        String contentType = myfile.getContentType(); //获得正文类型 → 文件类型
        long size = myfile.getSize(); //获得上传的文件大小

        //transferTo方法传入的参数，就是你当前这个文件保存的位置
        //File file = new File("D:\\WorkSpace\\j37_workspace","dlrb.jpg");
        File file = new File("D:\\WorkSpace\\j37_workspace",originalFilename);
        myfile.transferTo(file);
        return "upload success";
    }

    @RequestMapping("files")
    public String upload(MultipartFile[] myfiles) throws IOException {
        //遍历完成上传就行了
        for (MultipartFile myfile : myfiles) {
            String originalFilename = myfile.getOriginalFilename();//获得原始文件名，上传的文件的名字
            File file = new File("D:\\WorkSpace\\j37_workspace",originalFilename);
            myfile.transferTo(file);
        }
        return "success";
    }




}
